package com.test.lock;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierApp {

	public static void main(String[] args) {
		CyclicBarrier lock = new CyclicBarrier(6);
//		CyclicBarrier lock = new CyclicBarrier(5);
		for(int i=0;i<5;i++) {
			new Thread(()->{
				try {
					System.out.println("开始等待");
					lock.await();
					System.out.println("执行任务");
					Thread.sleep(2000);
					System.out.println("任务执行完毕");
				} catch (InterruptedException | BrokenBarrierException e) {
					e.printStackTrace();
				}
			}) .start();
		}
		try {
			Thread.sleep(5000);
			System.out.println("主线程通知开始执行任务");
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

	}

}
